Recaptcha Integration

  • Steps

    1. Generate recaptcha key

    1. Goto https://www.google.com/u/2/recaptcha/admin/create

    2. login with email id

    3. enter domain name

    4. get key and secret

    2. Save key and secret in laravel .env

    
                        RECAPTCHA_KEY=
                        RECAPTCHA_SECRET=
                        

    3. create the general.php file in config folder

    
                        return [
                            'RECAPTCHA_KEY' => env('RECAPTCHA_KEY', ''),
                            'RECAPTCHA_SECRET' => env('RECAPTCHA_SECRET', ''),
                        ];
                        

    3. add a hidden field between the form tag for recaptcha token

    
                          <form id="verifyform">
                            <input type="hidden" name="recaptcha_response" id="recaptchaResponse">
                          </form>
                        

    4. add recaptcha script at the bottom of the page

    
                        <script src="https://www.google.com/recaptcha/api.js?render={{config('general.RECAPTCHA_KEY')}}"></script>
                        

    5. call recaptcha function before submiting the form for token

    
                        grecaptcha.execute('{{config('general.RECAPTCHA_KEY')}}', { action: 'contact' }).then(function (token) {
                         
                          $('#recaptchaResponse').val(token);
                          
    
                          $("#verifyform").submit();
    
                        });
                      
    1. grecaptcha.execute() call recapcha api and return a token
    2. token set into hiden field recaptchaResponse
    3. use form submit() inside the grecaptcha.execute(); (submit() call after the response from recaptcha())

    6. verify the recapta token in the controller

    in the controller

    
                      public function verify(Request $r)
                        {
                               try{
                                      //set recaptcha api
                                      $recaptcha_url = 'https://www.google.com/recaptcha/api/siteverify';
                                      $recaptcha_secret = config('general.RECAPTCHA_KEY');
    
                                      //recaptcha token from the form (hidden field)
                                      $recaptcha_response = $input['recaptcha_response'];
                                      
                                      //verify recaptcha token using api 
                                      $recaptcha = file_get_contents($recaptcha_url . '?secret=' . $recaptcha_secret . '&response=' . $recaptcha_response);
                                      $recaptcha = json_decode($recaptcha);
    
                                      //if token is correct, api return success=true
                                      if ($recaptcha->success == true) {
                                          $isVerified = true;
                                      } else {
                                          $isVerified = false;
                                          return redirect()->back();
                                      }
                                  }
                                  catch(\Exception $e)
                                  {            
                                      return redirect()->back();
                                  }
    
                                  if($isVerified==true){
                                     // write your code here
                                     BasicBooking::create($input);
                                  }
    
                                  //redirect to a route you want
                                  return redirect()->route('booking.preview');
    
                        }